<template>
  <el-dialog title="选择会员" :visible.sync="visible" width="800px" append-to-body>
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="手机号" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员id" align="center" prop="id" />
      <el-table-column label="会员姓名" align="center" prop="nickname" />
      <el-table-column label="头像" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.avatarurl" alt="图片描述" width="50" height="50" />
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listSlCustom } from "@/api/projects/SlCustom";

export default {
  name: "CustomerSelect",
  props: {
    value: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 会员列表
      list: [],
      // 选中的会员
      selected: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tel: null
      }
    };
  },
  computed: {
    visible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit('input', val);
      }
    }
  },
  watch: {
    value(val) {
      if (val) {
        this.getList();
      }
    }
  },
  methods: {
    /** 获取会员列表 */
    getList() {
      this.loading = true;
      listSlCustom(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择变更 */
    handleSelectionChange(selection) {
      this.selected = selection.length === 1 ? selection[0] : null;
    },
    /** 确认选择 */
    confirmSelect() {
      if (!this.selected) {
        this.$modal.msgError("请选择一个会员");
        return;
      }
      const customer = {
        id: this.selected.id,
        nickname: this.selected.nickname,
        phone: this.selected.phone
      };
      this.$emit('select', customer);
      this.visible = false;
    },
    /** 取消按钮 */
    cancel() {
      this.visible = false;
    }
  }
};
</script> 